package cn.iocoder.yudao.module.promotion.service.points;

import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.promotion.controller.admin.points.pointsproduct.PointsProductBaseVO;
import cn.iocoder.yudao.module.promotion.controller.admin.points.pointsproduct.PointsProductCreateReqVO;
import cn.iocoder.yudao.module.promotion.controller.admin.points.vo.pointsactivity.PointsActivityCreateReqVO;
import cn.iocoder.yudao.module.promotion.controller.admin.points.vo.pointsactivity.PointsActivityExportReqVO;
import cn.iocoder.yudao.module.promotion.controller.admin.points.vo.pointsactivity.PointsActivityPageReqVO;
import cn.iocoder.yudao.module.promotion.controller.admin.points.vo.pointsactivity.PointsActivityUpdateReqVO;
import cn.iocoder.yudao.module.promotion.convert.points.PointsActivityConvert;
import cn.iocoder.yudao.module.promotion.convert.points.PointsProductConvert;
import cn.iocoder.yudao.module.promotion.dal.dataobject.points.PointsActivityDO;
import cn.iocoder.yudao.module.promotion.dal.dataobject.points.PointsProductDO;
import cn.iocoder.yudao.module.promotion.dal.dataobject.seckill.SeckillActivityDO;
import cn.iocoder.yudao.module.promotion.dal.mysql.points.PointsActivityMapper;
import cn.iocoder.yudao.module.promotion.dal.mysql.points.PointsProductMapper;
import cn.iocoder.yudao.module.promotion.dal.mysql.seckill.seckillactivity.SeckillActivityMapper;
import cn.iocoder.yudao.module.promotion.dal.mysql.seckill.seckillactivity.SeckillProductMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

import static cn.hutool.core.collection.CollUtil.newArrayList;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.POINTS_ACTIVITY_NOT_EXISTS;

/**
 * 积分活动 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class PointsActivityServiceImpl implements PointsActivityService {

    @Resource
    private PointsActivityMapper pointsActivityMapper;

    @Resource
    private PointsProductMapper pointsProductMapper;

    @Resource
    private SeckillActivityMapper seckillActivityMapper;

    @Resource
    private SeckillProductMapper seckillProductMapper;

/*    @Resource
    private ProductSpuApi productSpuApi;

    @Resource
    private ProductSkuApi productSkuApi;*/

    @Override
    public Long createPointsActivity(PointsActivityCreateReqVO createReqVO) {
        PointsActivityDO pointsActivity = PointsActivityConvert.INSTANCE.convert(createReqVO);
        pointsActivityMapper.insert(pointsActivity);
        // 校验商品是否存在
        if(createReqVO.getProductsList() != null && createReqVO.getProductsList().size() > 0){
            // 插入
            for(PointsProductCreateReqVO pointsProductBaseVO : createReqVO.getProductsList()){
                validateProductExists(pointsProductBaseVO);
                // 插入商品
                pointsProductBaseVO.setActivityId(pointsActivity.getId());
                pointsProductBaseVO.setActivityStartTime(pointsActivity.getStartTime());
                pointsProductBaseVO.setActivityEndTime(pointsActivity.getEndTime());
                pointsProductBaseVO.setPoints(createReqVO.getPoints());
                pointsProductBaseVO.setCombinationPrice(createReqVO.getPointsPrice());
                PointsProductDO pointsProductDO = PointsProductConvert.INSTANCE.convert(pointsProductBaseVO);
                pointsProductMapper.insert(pointsProductDO);
            }
        }
        // 返回
        return pointsActivity.getId();
    }

    /**
     * 校验拼团商品是否都存在
     *
     * @param pointsProductBaseVO 积分商品
     */
    private void validateProductExists(PointsProductBaseVO pointsProductBaseVO) {
        // 1. 校验商品 spu 是否存在
        /*ProductSpuRespDTO spu = productSpuApi.getSpu(pointsProductBaseVO.getSpuId());
        if (spu == null) {
            throw exception(SPU_NOT_EXISTS);
        }
        // 2. 校验商品 sku 都存在
        ProductSkuRespDTO sku = productSkuApi.getSku(pointsProductBaseVO.getSkuId());
        if (sku == null) {
            throw exception(SKU_NOT_EXISTS);
        }*/
    }

    @Override
    public void updatePointsActivity(PointsActivityUpdateReqVO updateReqVO) {
        // 校验存在
        validatePointsActivityExists(updateReqVO.getId());
        // 更新
        PointsActivityDO updateObj = PointsActivityConvert.INSTANCE.convert(updateReqVO);
        pointsActivityMapper.updateById(updateObj);
        // 校验商品是否存在
        if(updateReqVO.getProductsList() != null && updateReqVO.getProductsList().size() > 0){
            //清空之前的商品数据
            List<PointsProductDO> productsList = getPointsProductListByActivityIds(newArrayList(updateReqVO.getProductsList().get(0).getActivityId()));
            List<Long> productsIds = newArrayList();
            for(PointsProductDO productDO :productsList){
                productsIds.add(productDO.getId());
            }
            if(productsList.size() > 0){
                pointsProductMapper.deleteBatchIds(productsIds);
            }
            // 插入
            for(PointsProductCreateReqVO pointsProductBaseVO : updateReqVO.getProductsList()){
                validateProductExists(pointsProductBaseVO);
                // 插入商品
                pointsProductBaseVO.setActivityId(updateObj.getId());
                pointsProductBaseVO.setActivityStartTime(updateObj.getStartTime());
                pointsProductBaseVO.setActivityEndTime(updateObj.getEndTime());
                pointsProductBaseVO.setPoints(updateReqVO.getPoints());
                pointsProductBaseVO.setCombinationPrice(updateReqVO.getPointsPrice());
                PointsProductDO pointsProductDO = PointsProductConvert.INSTANCE.convert(pointsProductBaseVO);
                pointsProductMapper.insert(pointsProductDO);
            }
        }
    }

    @Override
    public void deletePointsActivity(Long id) {
        // 校验存在
        validatePointsActivityExists(id);
        // 删除
        pointsActivityMapper.deleteById(id);
    }

    private void validatePointsActivityExists(Long id) {
        if (pointsActivityMapper.selectById(id) == null) {
            throw exception(POINTS_ACTIVITY_NOT_EXISTS);
        }
    }

    @Override
    public PointsActivityDO getPointsActivity(Long id) {
        return pointsActivityMapper.selectById(id);
    }

    @Override
    public List<PointsProductDO> getPointsProductListByActivityIds(Collection<Long> activityIds) {
        return pointsProductMapper.selectListByActivityIds(activityIds);
    }

    @Override
    public List<PointsActivityDO> getPointsActivityList(Collection<Long> ids) {
        return pointsActivityMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<PointsActivityDO> getPointsActivityPage(PointsActivityPageReqVO pageReqVO) {
        return pointsActivityMapper.selectPage(pageReqVO);
    }

    @Override
    public List<PointsActivityDO> getPointsActivityList(PointsActivityExportReqVO exportReqVO) {
        return pointsActivityMapper.selectList(exportReqVO);
    }
    @Override
    public PageResult<PointsActivityDO> getPointsActivityPage(PageParam pageParam) {
        return pointsActivityMapper.selectPage(pageParam, CommonStatusEnum.ENABLE.getStatus());
    }

    @Override
    public List<PointsActivityDO> pointsList(PointsActivityPageReqVO reqVO) {
        return pointsActivityMapper.selectList(reqVO);
    }

    @Override
    public List<SeckillActivityDO> getSeckillActivityListByIds(Collection<Long> activityIds) {
        return seckillActivityMapper.getSeckillActivityListByIds(activityIds);
    }

    @Override
    public void deleteBySpuId(Long spuId) {
        // 删除
        seckillProductMapper.deleteBySpuId(spuId);
    }
}
